/* -----------------------------------------------------------------------
 * Demonstration of how to write unit tests for dominion-base
 * Include the following lines in your makefile:
 *
 * unittest4: unittest4.c dominion.o rngs.o
 *      gcc -o unittest4 -g  unittest4.c dominion.o rngs.o $(CFLAGS)
 *
 *
 *this assumes that scoreFor() works
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "rngs.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int main() {
    int seed = 1000;
	int maxTests = 2;
    int numPlayer = MAX_PLAYERS;
    int p, r, i, tests;
	int pa[MAX_PLAYERS];
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;
    
	//gives weird message at end
	srand(seed);
	printf ("TESTING getWinners():\n");
	
	//run getWinners()
	//if our asserts pass we know it worked.
	//questions: do i need to initialize the game?	yes
	//just look at state->supplyCount[]	
	
	//get array of players
	for(p = 0; p < numPlayer; p++){
		pa[p] = 0;
	}

    for (tests = 1; tests <= maxTests; tests++)
    {
        for (p = 0; p < numPlayer; p++)
        {
            
            //not sure this is necessary
			memset(&G, 23, sizeof(struct gameState));   // clear the game state
            r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
			
			//set discardCount
			for(i = 0; i < numPlayer; i++){
				G.discardCount[i] = 2;
			}
			G.deck[p][0] = estate;		//set province to 0
			r = getWinners(pa,&G);		//get the winners
			
#if (NOISY_TEST == 1)
            printf("Player%d should be winner.\n", p);
			for(i = 0; i < numPlayer; i++){
				if(pa[i] == 1){
					printf("player%d is a winner\n", i);
				}
			}
#endif
            assert(pa[p] == 1); 				// check if isGameOver is correct
			
			if((p+1) < numPlayer){
				G.deck[p+1][0] = province;		//make player p+1 winner
			}else{
				G.deck[p-1][0] = province;		//make player p-1 winner
			}
			r = getWinners(pa,&G);				//get the winners
					
#if (NOISY_TEST == 1)
			if((p+1) < numPlayer){
				printf("Player%d should be winner.\n", (p+1));
			}else{
				printf("Player%d should be winner.\n", (p-1));
			}
			for(i = 0; i < numPlayer; i++){
				if(pa[i] == 1){
					printf("player%d is a winner\n", i);
				}
			}
#endif		
			assert(pa[p-1] == 1 || pa[p+1] == 1);
			
			G.deck[p][0] = province;			//players p and above are winners
			//G.deck[p+1][0] = province;		//make player p+1 winner
			r = getWinners(pa,&G);				//get the winners
#if (NOISY_TEST == 1)
			if((p+1) < numPlayer){
				printf("Player%d and Player%d should be winners.\n", (p+1), p);
			}else{
				printf("Player%d and Player%d should be winners.\n", (p-1), p);
			}
			for(i = 0; i < numPlayer; i++){
				if(pa[i] >= 1){
					printf("player%d is a winner and pa[i] = %d pa[i+1] = %d pa[i-1] = %d\n", i, pa[i], pa[i+1], pa[i-1]);
				}
			}
#endif

			assert(pa[p] == 1 && (pa[p+1] == 1 || pa[p-1] == 1));
			
		}
	}
    printf("All tests passed!\n");

    return 0;
}
